package bsf.baseservice.businessmq.producter;

import bsf.baseservice.businessmq.model.tb_datanode_model;
import bsf.baseservice.businessmq.model.tb_mqpath_model;
import bsf.baseservice.businessmq.model.tb_producter_model;
import bsf.baseservice.businessmq.systemruntime.PartitionIDInfo;
import bsf.baseservice.businessmq.systemruntime.PartitionRuleHelper;
import bsf.baseservice.businessmq.model.tb_mqpath_partition_model;
import bsf.baseservice.businessmq.producter.loadbalance.BaseLoadBalance;
import bsf.baseservice.businessmq.producter.loadbalance.LoadBalancePartitionInfo;
import bsf.baseservice.businessmq.systemruntime.log.ErrorLogHelper;

public class ProducterInfo
{
	public tb_producter_model ProducterModel;

	public tb_mqpath_model MqPathModel;

	public java.util.ArrayList<tb_mqpath_partition_model> MqPathParitionModel;

	public java.util.HashMap<Integer, tb_datanode_model> DataNodeModelDic;

	public BaseLoadBalance LoadBalance;

	private Object operatorlock = new Object();

	/** 
	 //顺序轮询节点获取节点及分区,从而达到负载均衡的目的
	 @return 
	*/
	public final LoadBalanceNodeInfo getLoadBalanceNodeInfo()
	{
		try
		{
			synchronized (operatorlock)
			{
				LoadBalanceNodeInfo info = new LoadBalanceNodeInfo();
				LoadBalancePartitionInfo p = LoadBalance.getLoadBalancePartitionInfo();
				if (p == null)
				{
					return null;
				}
				PartitionIDInfo partitionidinfo = PartitionRuleHelper.getPartitionIDInfo(p.PartitionId);
				info.DataNodeModel=DataNodeModelDic.get(partitionidinfo.DataNodePartition);
				info.MQPathPartitionModel=p.MQPathParitionModel;
				return info;
			}
		}
		catch (Exception exp)
		{
			ErrorLogHelper.writeLine(-1, "", "GetLoadBalanceNodeInfo", "生产者负载均衡出错", exp);
			throw exp;
		}
	}
	/** 
	 更新生产者缓存信息
	 
	 @param productinfo
	*/
	public  void update(ProducterInfo productinfo)
	{
		synchronized (operatorlock)
		{
			this.MqPathModel=productinfo.MqPathModel;
			this.MqPathParitionModel=productinfo.MqPathParitionModel;
			this.DataNodeModelDic=productinfo.DataNodeModelDic;
			this.LoadBalance.clearError();
		}
	}
	/** 
	 移除生产者某个节点相关的分区信息
	 
	 @param datanodepartition
	*/
	public  void removeMQPathPartition(int datanodepartition)
	{
		synchronized (operatorlock)
		{
			java.util.ArrayList<tb_mqpath_partition_model> remove = new java.util.ArrayList<tb_mqpath_partition_model>();
			for (tb_mqpath_partition_model p : MqPathParitionModel)
			{
				PartitionIDInfo partitionidinfo = PartitionRuleHelper.getPartitionIDInfo(p.partitionid);
				if (partitionidinfo.DataNodePartition == datanodepartition)
				{
					remove.add(p);
				}
			}

			for (tb_mqpath_partition_model p : remove)
			{
				MqPathParitionModel.remove(p);
			}
		}
	}
}